/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WebServices;

/**
 *
 * @author Usuario
 */

import Hndlr.HndlrGrupo;
import Hndlr.HndlrUsuario;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.Endpoint;
import objetos.Comentario;
import objetos.Contenido;

/**
 *
 * @author Jota
 */

@WebService
@SOAPBinding(style = Style.RPC, parameterStyle = ParameterStyle.WRAPPED)

public class CtrlComentarioWS{

     
    private Endpoint endpoint = null;
    private Logger log = Logger.getLogger(CtrlAgregarEntradaMuroWS.class.getName());

    
    @WebMethod(exclude = true)
    public void publicar() {
        Propiedades propiedades = Propiedades.getInstance();
        String nomCtrl = "CtrlComentario";
        String camino = "http://" + propiedades.getServer() + ":" + propiedades.getPuerto() + "/" + nomCtrl;
        log.info("publicando el endpoint "+nomCtrl);
        endpoint = Endpoint.publish(camino, this);
        log.info("Endpoint "+nomCtrl+" publicado en " + camino);
    }
    
      @WebMethod(exclude = true)
    public Endpoint getEndpoint() {
        return endpoint;
    }
    
    
    @WebMethod
    public void agregarComentario(@WebParam(name = "correoUsrMuro")String correoUsrMuro,@WebParam(name = "correoUsr") String correoUsr,@WebParam(name = "refCont") String refCont,@WebParam(name = "coment") String coment,@WebParam(name = "esMuroGurpo") boolean esMuroGrupo) {
        
        ArrayList<Contenido> listCont = null;
        
        if (esMuroGrupo)
            listCont = HndlrGrupo.getInstance().obtenerGrupo(correoUsrMuro).getMuro().getContenidosObjeto();
        else
            listCont = HndlrUsuario.getInstance().obtenerUsuario(correoUsrMuro).getMuro().getContenidosObjeto();
        
        Iterator<Contenido> itera = listCont.iterator();
        Contenido cont = null;
        Comentario com = new Comentario(coment, correoUsr);
        while (itera.hasNext()){
            cont = itera.next();
            String ref = cont.getTitulo()+cont.getFechaPublicacion().getTime().toString();
            if(ref.equals(refCont)){
                cont.getComentarios().add(com);
            }
        }
    }
    
    @WebMethod
    public void eliminarComentario(@WebParam (name = "deuenoMuro")String duenoMuro,@WebParam (name = "titulo") String tituloCont,@WebParam (name = "coment") String coment,@WebParam (name = "esMuroGrupo") boolean esMuroGrupo) {
        
        
        if (esMuroGrupo)
            HndlrGrupo.getInstance().obtenerGrupo(duenoMuro).getMuro().eliminarComentario(tituloCont, coment);
        else
            HndlrUsuario.getInstance().obtenerUsuario(duenoMuro).getMuro().eliminarComentario(tituloCont, coment);
        
        
    }
    
}

